package kdone;

import java.util.ArrayList;


public class ProtocolKD {
    
    ProtocolKD_Define PrtDef = new ProtocolKD_Define();
    ArrayList<Integer> lProtocol = new ArrayList<Integer>();
    
    int Status = PrtDef.STATUS_NORMAL;
    int Count  = 0;
    
    public ProtocolKD() {
        Status = PrtDef.STATUS_NORMAL;
    }

    public ProtocolKD( String protocol ){
        int length = protocol.length();
        for ( int i = 0; i < length; i += 2 )
        {
            
        }
        Status = PrtDef.STATUS_ANALYSIS;
        
    }
    
    public ProtocolKD( ArrayList<String> protocol ){
    
        Status = PrtDef.STATUS_ANALYSIS;
    }
    
    public boolean PutDataChar( int tData ){
        int CheckSum = 0, AddSum = 0;
        
        if( Status == PrtDef.STATUS_NORMAL){
            if( tData == PrtDef.FORMAT_DATA_STX ){
                Status = PrtDef.STATUS_RECEIVE;
                lProtocol.clear();
                Count = 0;
            }
        }

        if( Status == PrtDef.STATUS_RECEIVE ){
            lProtocol.add(tData);
            Count = lProtocol.size();
            if((Count > PrtDef.FORMAT_INDEX_LENGTH) 
                    && ( Count == (lProtocol.get(PrtDef.FORMAT_INDEX_LENGTH)+7))){
                for(int i = 0; i < (Count - 2); i++){
                    CheckSum ^= lProtocol.get(i);
                    AddSum += lProtocol.get(i);
                }
                if((CheckSum == lProtocol.get(Count-1)) && (AddSum == lProtocol.get(Count))){
                    Status = PrtDef.STATUS_RECEIVE_OK;
                }
                Status = PrtDef.STATUS_RECEIVE_FAIL;
                return true;
            }
        }
        return false;
    }
    
    public boolean IsReceiveOk()
    {
        if( Status == PrtDef.STATUS_RECEIVE_OK ) return true;
        return false;
    }
    
    public int GetLineDevice(){
        return lProtocol.get(PrtDef.FORMAT_INDEX_DEVICE);
    }
    
    public int GetTxDevice(){
        return lProtocol.get(PrtDef.FORMAT_INDEX_TX_DEVICE);
    }
    
    public int GetRxDevice(){
        return lProtocol.get(PrtDef.FORMAT_INDEX_RX_DEVICE);
    }

    public int GetTxDong(){
        int tReturn;
        tReturn = lProtocol.get(PrtDef.FORMAT_INDEX_TX_DONG_H) << 8;
        tReturn += lProtocol.get(PrtDef.FORMAT_INDEX_TX_DONG_L);
        return tReturn;
    }
    
    public int GetTxHoo(){
        int tReturn;
        tReturn = lProtocol.get(PrtDef.FORMAT_INDEX_TX_HOO_H) << 8;
        tReturn += lProtocol.get(PrtDef.FORMAT_INDEX_TX_HOO_L);
        return tReturn;
    }
    
    public int GetRxDong(){
        int tReturn;
        tReturn = lProtocol.get(PrtDef.FORMAT_INDEX_RX_DONG_H) << 8;
        tReturn += lProtocol.get(PrtDef.FORMAT_INDEX_RX_DONG_L);
        return tReturn;
    }
    
    public int GetRxHoo(){
        int tReturn;
        tReturn = lProtocol.get(PrtDef.FORMAT_INDEX_RX_HOO_H) << 8;
        tReturn += lProtocol.get(PrtDef.FORMAT_INDEX_RX_HOO_L);
        return tReturn;
    }
    


}
